﻿using System;
using System.Threading;
using System.Diagnostics;
using System.IO;

namespace TwipPlays
{

    public class IRCThread : IDisposable
    {

        private Process process;
        private Form1 window;

        public bool disposed = false;
        public string server;
        public string user;
        public string auth;
        public string channel;

        public IRCThread(Form1 window)
        {

            this.window = window;

            string filepath = "irc_parser/";
            filepath = System.IO.Path.GetFullPath(filepath);

            process = new Process();
            process.StartInfo.FileName = filepath + "irc_parser.exe";
            process.StartInfo.Arguments = "\"" + filepath + "config.ini" + "\"";
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardError = true;

        }

        public void StartIRC()
        {

            this.process.Start();

            while (!(this.process.HasExited))
            {

                if (process.StandardOutput != null && process.StandardOutput.ToString() != "")
                {

                    StreamReader streamReader = process.StandardOutput;
                    string outString = streamReader.ReadLine();

                    if (outString == null || outString == "")
                        continue;

                    // split the string into array for user:action
                    string[] separators = new string[] { " " };
                    string[] userActionArr = outString.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                    if (userActionArr.Length == 2)
                    {

                        Object[] args = { userActionArr[0], userActionArr[1] };
                        this.window.Invoke(window.addActionDelegate, args);

                    }
                    else if (userActionArr.Length == 3)
                    {

                        if ( userActionArr[0] == "vote" && userActionArr[1] == "count")
                        {

                            // vote count strings are for vote mode updates
                            int count = 0;
                            string countStr = userActionArr[2];
                            int.TryParse(countStr, out count);

                            Object[] args = { count };
                            this.window.Invoke(window.updateVotingDelegate, args);

                        }
                        else if (userActionArr[0] == "vote" && userActionArr[1] == "mode")
                        {

                            int mode = 0;
                            string modeStr = userActionArr[2];
                            int.TryParse(modeStr, out mode);

                            Object[] args = { mode };
                            this.window.Invoke(window.changeVotingModeDelegate, args);

                        }
                        else if (userActionArr[0] == "vote" && userActionArr[1] == "action")
                        {

                            this.window.Invoke(window.voteActionClearDelegate);

                        }
                        else if (userActionArr[0] == "voted" && userActionArr[1] == "action")
                        {

                            Object[] args = { userActionArr[2] };
                            this.window.Invoke(window.setVotedActionDelegate, args);

                        }

                    }
                    else if (userActionArr.Length == 4)
                    {

                        if (userActionArr[0] == "vote" && userActionArr[1] == "action")
                        {

                            string actStr = userActionArr[2];
                            string votes = userActionArr[3];

                            // userActionArr[2] = action
                            // userActionArr[3] = votes
                            Object[] args = { userActionArr[2], userActionArr[3] };
                            this.window.Invoke(window.addVoteActionDelegate, args);

                        }

                    }

                }

            }

        }

        public void CloseIRC()
        {

            this.process.Kill();					// An inifinitely-looping program, the IRC client must be killed (or coded for other process signals)

        }

        public void Dispose()
        {

            this.Dispose(true);

        }

        protected virtual void Dispose(bool disposing)
        {

            if (this.disposed)
                return;

            if (disposing)
            {

                this.process.Dispose();

            }

            this.disposed = true;

        }

    }

}

